package com.tsd.jxc.fin.controller;

import com.tsd.common.controller.BaseController;
import com.tsd.core.annotation.CheckBind;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.fin.FinanceConstants;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestDetailExt;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestExt;
import com.tsd.jxc.fin.service.JxcFinancePaymentRequestService;
import com.tsd.jxc.fin.vo.PaymentRequestExportVo;
import com.tsd.jxc.req.JxcFinancePaymentRequestDetailReqParamsVo;
import com.tsd.jxc.req.JxcFinancePaymentRequestReqParamsVo;
import com.tsd.jxc.setting.service.JxcSettingService;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SystemSettingService;
import com.tsd.system.vo.GlobalSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcFinancePaymentRequest财务-收付款请求单 控制层
 *
 * @author Hillpool
 * @date 2023/02/09 18:00:21
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-财务-收付款请求单", description = "财务-收付款请求单的API描述")
public class JxcFinancePaymentRequestController extends BaseController {

    @Resource
    private JxcFinancePaymentRequestService jxcFinancePaymentRequestService;
    @Resource
    private SystemSettingService systemSettingService;
    @Resource
    private JxcSettingService jxcSettingService;

    @ApiOperation(value = "根据sid获取财务-收付款请求单", notes = "根据sid获取财务-收付款请求单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "财务-收付款请求单sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcFinancePaymentRequestBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getJxcFinancePaymentRequestBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            JxcFinancePaymentRequestExt requestExt = jxcFinancePaymentRequestService.queryBySid(sid);
            JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(DataSourceContextHolder.getDataSourceType());
            if (settingVo.isClothing()) {
                hr.setData(JxcFinancePaymentRequestReqParamsVo.transfer(requestExt, settingVo));
            } else {
                hr.setData(requestExt);
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除财务-收付款请求单", notes = "根据id删除财务-收付款请求单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "财务-收付款请求单sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcFinancePaymentRequests.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcFinancePaymentRequests() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            List<String> list = super.getParamsList("sid_list", String.class, true);
            jxcFinancePaymentRequestService.deleteJxcFinancePaymentRequests(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存财务-收付款请求单", notes = "保存财务-收付款请求单，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "财务-收付款请求单", required = true, paramType = "body", dataType = "JxcFinancePaymentRequest")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcFinancePaymentRequest.action", method = RequestMethod.POST)
    public HttpResult<Object> saveJxcFinancePaymentRequest() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            JxcFinancePaymentRequestReqParamsVo paramsVo = super.getParams(request, JxcFinancePaymentRequestReqParamsVo.class);
            jxcFinancePaymentRequestService.saveJxcFinancePaymentRequest(paramsVo.convert(), opUser, !HlpUtils.isEmpty(paramsVo.getIsXd()));
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询财务-收付款请求单", notes = "分页查询财务-收付款请求单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcFinancePaymentRequests.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcFinancePaymentRequests() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "type", "type", true, false);
            HlpUtils.copyStringValue(map, params, "code", "code", false, false);
            HlpUtils.copyStringValue(map, params, "client_sid", "client_sid", false, false);
            HlpUtils.copyStringValue(map, params, "supplier_sid", "supplier_sid", false, false);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            HlpUtils.copyIntegerValue(map, params, "audit_state", "audit_state", false, null);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<JxcFinancePaymentRequestExt> list = jxcFinancePaymentRequestService.findJxcFinancePaymentRequests(params, page);
            if (!HlpUtils.isEmptyList(list)) {
                GlobalSetting setting = systemSettingService.getGlobalSetting();
                ListUtil.forEach(list, t -> setting.procDisplay(t, GlobalSetting.TYPE_CLIENT));
                ListUtil.forEach(list, t -> setting.procDisplay(t, GlobalSetting.TYPE_SUPPLIER));
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 审核/反审核
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/changeJxcFinancePaymentRequestAuditState.action", method = RequestMethod.POST)
    @CheckBind(person = true)
    public HttpResult<Object> changeJxcFinancePaymentRequestAuditState() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Integer state = HlpUtils.getInteger(map, "state");
            super.checkEmpty(state, "请提供参数：state");
            if (!Arrays.asList(1, -1).contains(state)) {
                throw new HlpException("state值错误：1-审核、-1-反审核");
            }
            List<String> sidList = super.getParamsList(map, "sid_list", String.class, true);
            jxcFinancePaymentRequestService.changeJxcFinancePaymentRequestAuditState(sidList, state, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 作废
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cancelJxcFinancePaymentRequest.action", method = RequestMethod.POST)
    @CheckBind(person = true)
    public HttpResult<Object> cancelJxcFinancePaymentRequest() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            List<String> sidList = super.getParamsList("sid_list", String.class, true);
            jxcFinancePaymentRequestService.cancelJxcFinancePaymentRequest(sidList, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 获取上期数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInitRequestFormData.action", method = RequestMethod.POST)
    public HttpResult<Object> getInitRequestFormData() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            JxcFinancePaymentRequestExt requestExt = jxcFinancePaymentRequestService.getInitRequestFormData(map);
            hr.setData(requestExt);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 获取选单数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getJxcFinancePaymentRequestDetail4Select.action", method = RequestMethod.POST)
    public HttpResult<Object> getJxcFinancePaymentRequestDetail4Select() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyStringValue(map, params, "type", "type", true, false);
            HlpUtils.copyStringValue(map, params, "client_sid", "client_sid", false, false);
            HlpUtils.copyStringValue(map, params, "supplier_sid", "supplier_sid", false, false);
            HlpUtils.copyStringValue(map, params, "request_sid", "request_sid", false, false);
            HlpUtils.copyListValue(map, params, "selected_list", "selected_list", false, String.class);
            String type = HlpUtils.getString(map, "type");
            if (FinanceConstants.TYPE_BUY.equals(type)) {
                SysUserExt opUser = super.getSysUser();
                params.put("group_sid", opUser.getGroup_sid());
            }
            List<JxcFinancePaymentRequestDetailExt> detailExtList = jxcFinancePaymentRequestService.getJxcFinancePaymentRequestDetail4Select(params);
            JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(DataSourceContextHolder.getDataSourceType());
            if (settingVo.isClothing()) {
                hr.setData(JxcFinancePaymentRequestDetailReqParamsVo.transfer(detailExtList, settingVo));
            } else {
                hr.setData(detailExtList);
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 获取导出数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getRequestExportData.action", method = RequestMethod.POST)
    public HttpResult<Object> getRequestExportData() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            PaymentRequestExportVo vo = jxcFinancePaymentRequestService.getRequestExportData(map, opUser);
            hr.setData(vo);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
